package com.ljkj.cordial.chat.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 提示
 */
public class CommonHintDialog extends Dialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_left_submit)
    TextView tvLeftSubmit;
    @BindView(R.id.tv_right_submit)
    TextView tvRightSubmit;
    @BindView(R.id.ll_warn)
    LinearLayout llWarn;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    private Unbinder unbinder;

    public CommonHintDialog(@NonNull Context context) {
        super(context, com.nic.view.R.style.dialog_no_bg);
        setContentView(R.layout.dialog_common_hint);
        unbinder = ButterKnife.bind(this);
        setCancelable(false);
        initLayout();
        //按空白处不能取消动画
//        setCanceledOnTouchOutside(true);
        //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
//        getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp8 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getContext().getResources().getDisplayMetrics());
        getWindow().getDecorView().setPadding(dp8, 0, dp8, 0);
    }

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//    }

    private void setBackground(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    @Override
    public void show() {
        super.show();
//        setBackground(0.4f);
    }

    public void showBottom() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        show();
    }

    public CommonHintDialog visiableWarn() {
        llWarn.setVisibility(View.VISIBLE);
        return this;
    }

    public CommonHintDialog setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    public CommonHintDialog setContent(String content) {
        tvContent.setVisibility(View.VISIBLE);
        tvContent.setText(content);
        return this;
    }

    public CommonHintDialog setLeftSubmit(String content, final View.OnClickListener listener) {
        tvLeftSubmit.setText(content);
        tvLeftSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });

        return this;
    }

    public CommonHintDialog setRightSubmit(String content, final View.OnClickListener listener) {
        tvRightSubmit.setText(content);
        tvRightSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
        return this;
    }

    public void initLayout() {
//        if (ChatApp.getInstance().getFontScale() > ChatApp.getInstance().FONT_NORMAL) {
//            llBottom.setOrientation(LinearLayout.VERTICAL);
//            int dp8 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, llBottom.getResources().getDisplayMetrics());
//            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvRightSubmit.getLayoutParams();
//            params.leftMargin = 0;
//            params.topMargin = dp8;
//            params.width = LinearLayout.LayoutParams.MATCH_PARENT;
//            tvLeftSubmit.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
//        }

    }

    public CommonHintDialog setRightSubmitGray(String content, View.OnClickListener listener) {
        tvRightSubmit.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bt_gray_circle_angle));
        tvRightSubmit.setTextColor(ContextCompat.getColor(getContext(), R.color.green5));
        tvRightSubmit.setText(content);
        if (listener != null) {
            tvRightSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    listener.onClick(v);
                }
            });
        }
        return this;
    }

    public CommonHintDialog setSingleSubmitGray(String content, final View.OnClickListener listener) {
        tvLeftSubmit.setText(content);
        int dp8 = getContext().getResources().getDimensionPixelOffset(R.dimen.dimen_8);
        tvLeftSubmit.setPadding(0, dp8, 0, dp8);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvLeftSubmit.getLayoutParams();
        params.width = LinearLayout.LayoutParams.MATCH_PARENT;
        params.leftMargin = dp8;
        params.rightMargin = dp8;
        tvLeftSubmit.setGravity(Gravity.CENTER);
        tvLeftSubmit.setLayoutParams(params);
        tvLeftSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
        tvRightSubmit.setVisibility(View.GONE);
        return this;
    }


    @Override
    public void dismiss() {
        super.dismiss();
        unbinder.unbind();
//        setBackground(1.0f);
    }


}
